package dao.mysql;

import java.sql.*;
import java.util.ArrayList;


import model.UserDTO;
import dao.DAOException;
import dao.IUserDAO;

public class UserDAO implements IUserDAO {
	
	MyJNDIConnection myConn;
	Connection con;
	PreparedStatement pst;
	
	public UserDAO(){
		myConn = new MyJNDIConnection();
	}
	
	public UserDTO findUser(int userID) throws DAOException {
		return new UserDTO();
	}
	public ArrayList<UserDTO> findAllUser() throws DAOException{
		ArrayList<UserDTO> items = new ArrayList<UserDTO>();
		String selectSql = "SELECT * FROM tblUser";
		try{
			con = myConn.getJNDIConnection();
			Statement st = con.createStatement();
			
			ResultSet rs = st.executeQuery(selectSql);
			while ( rs.next()){
				UserDTO user = new UserDTO();
				user.setuName(rs.getString(""));
				user.setuPassword(rs.getString(""));
				user.setDOB(rs.getDate(""));
				user.setGender(rs.getBoolean(0));
				items.add(user);
			}
			
			st.close();
		}
		catch(SQLException ex){
			String error = "Error selecting users. Nested Exception is : " + ex;
			throw new DAOException();
		}
		return items;
	}
	public void insertUser(UserDTO user)  {
		System.out.println("Heyeye");
		String insertSql = "INSERT INTO TBLUSER (username, password, DOB, Gender, Email) " +
							" VALUES (?,?,?,?,?)";
		try{
			con = myConn.getJNDIConnection();
		    pst = con.prepareStatement(insertSql);
			
			System.out.println("Before update : ");
			pst.setString(1, user.getuName());
			pst.setString(2, user.getuPassword());
			pst.setString(3, user.getDOB().toString());
			pst.setBoolean(4, user.getGender());
			pst.setString(5, user.getEmail());
			
			pst.execute();
			con.commit();
			
			
			
		}
		catch (SQLException ex){
			
			String error = "Error Inserting into User. Exception is : " + ex;

		}
		finally{
			try{
				pst.close();
				con.close();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	public void updateUser(UserDTO user) throws DAOException{
		
	}
	public void deleteUser(int userID) throws DAOException{
		
	}
}


